package com.jzg.jcptadmin.presenter;

import com.jzg.jcptadmin.Utils.SubmitValueVerification;
import com.jzg.jcptadmin.app.AppContext;
import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.constant.Constant;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.City;
import com.jzg.jcptadmin.data.vo.CityList;
import com.jzg.jcptadmin.data.vo.Province;
import com.jzg.jcptadmin.data.vo.ProvinceList;
import com.jzg.jcptadmin.viewinterface.AreaInterface;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by wangyd on 15/12/23.
 * 城市列表逻辑
 */
public class AreaPresenter extends BasePresenter<AreaInterface> {
    private final DataManager mDataManager;
    private Subscription mSubscription;
    private AreaInterface mView;

    public AreaPresenter(DataManager instance) {
        super();
        mDataManager = instance;
    }

    @Override
    public void attachView(AreaInterface mvpView) {
        super.attachView(mvpView);
        this.mView = mvpView;


    }

    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null) mSubscription.unsubscribe();
    }

    /**
     * 加载品牌数据
     */
    public void loadProvince() {
        checkViewAttached();
        Map<String, String> params = mView.getProvinceParams();
        if (SubmitValueVerification.isEmpty(params)) return;
        if (!AppContext.isNetWork) return;
        mSubscription = mDataManager.loadProvince(params).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io()).subscribe(new Observer<ProvinceList>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if (e != null&&mView!=null) {
                    mView.showError(e.toString());
                }
            }

            @Override
            public void onNext(ProvinceList provinceList) {
//                Logger.i(provinceList.toString());
                if (Constant.SUCCESS == provinceList.getStatus()) {
                    ArrayList<Province> provinces = provinceList.getContent();
                    for (Province p : provinces) {
                        p.setFontColor(mView.getDefaultFontColor());
                    }
                    mView.showProvince(provinces);
                } else {
                    mView.showError(provinceList.getMsg());
                }
            }
        });
    }

    /**
     * 查询市
     */
    public void loadCity() {
        checkViewAttached();
        Map<String, String> params = mView.getCityParams();
        if (SubmitValueVerification.isEmpty(params)) return;
        if (!AppContext.isNetWork) return;
        mSubscription = mDataManager.loadCity(params).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<CityList>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.toString());
            }

            @Override
            public void onNext(CityList cityList) {
                if (cityList.getStatus() == Constant.SUCCESS) {
                    List<City> cities = cityList.getContent();
                    for (City c : cities) {
                        c.setFontColor(mView.getDefaultFontColor());

                    }
                    //删除全部地区项
                    cities.remove(0);
                    //显示市列表
                    mView.showCity(cities);
                } else {
                    mView.showError(cityList.getMsg());
                }
            }
        });
    }

}

